/*************************************************************************
	> File Name: client.c
	> Author: life
	> Created Time: Mon 26 Aug 2019 02:39:26 PM CST
 ************************************************************************/
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define SEND 0 

/* socket
 * connect
 * send/recv
 */

int main(int argc,char**argv)
{
	int client_fd;
	int iError;
	struct sockaddr_in ServerAddr;
	struct sockaddr_in ClientAddr;
	char SendBUF[1000];
	int sendlen;

	if (argc!=2)
	{
		printf("<server ip>\n");
		return -1;
	}

	client_fd = socket(AF_INET,SOCK_DGRAM,0);

	ServerAddr.sin_family 		= AF_INET;
	ServerAddr.sin_port 		= htons(8080);
	//ServerAddr.sin_addr.s_addr 	= htonl(INADDR_ANY);
	iError = inet_aton(argv[1], &ServerAddr.sin_addr);
	if (iError == 0)
	{
		return -1;
	}
	memset(ServerAddr.sin_zero , 0 ,8);
#if SEND	
	iError = connect( client_fd, (const struct sockaddr *)&ServerAddr,sizeof(struct sockaddr_in));
	if (iError < 0)
	{
		printf("connect fild\n");
		return -1;
	}
#endif	
	while(1)
	{
        if(fgets(SendBUF,999,stdin))
        {
#if SEND
        	sendlen = send(client_fd, SendBUF, strlen(SendBUF), 0);
#else
			sendlen = sendto(client_fd, SendBUF,strlen(SendBUF) , 0,(struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in));
#endif
        	if(sendlen < 0)
        	{
        		close(client_fd);
        		return -1;
        	}
        }
	}		
	return 0;
}
